package com.cms.recursoshumanos.popup;

import com.cms.recursoshumanos.component.base.ProgressAnimationField;
import com.cms.recursoshumanos.engine.BitmapManager;
import com.cms.recursoshumanos.utilities.ScreenProperties;

import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.VirtualKeyboard;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class LoadingPopupScreen extends PopupScreen implements FieldChangeListener
{
	public static int Boton = 0;
	public static int Previa = 1;
	
	private LabelField texto;
	private ProgressAnimationField _animatedBitmapField;
	private ButtonField botonOk;
	private ButtonField botonVer;
	
	public LoadingPopupScreen()
	{
		super(new VerticalFieldManager(USE_ALL_WIDTH | FIELD_HCENTER));
		_animatedBitmapField = new ProgressAnimationField(BitmapManager.getInstance().getSpinner(),6,FIELD_HCENTER | DrawStyle.HCENTER);
		_animatedBitmapField.setMargin(10, 0, 10, 0);
		texto = new LabelField("",USE_ALL_WIDTH | FIELD_HCENTER | DrawStyle.HCENTER);
		texto.setFont(ScreenProperties.normalFieldFontBold);
		add(_animatedBitmapField);
		add(texto);
		if (this.getScreen().getVirtualKeyboard().isSupported())
		{
			this.getScreen().getVirtualKeyboard().setVisibility(VirtualKeyboard.HIDE_FORCE);
		}
	}
	
	public LoadingPopupScreen(int tipo, String text)
	{
		super(new VerticalFieldManager(USE_ALL_WIDTH | FIELD_HCENTER));
		if(tipo == Boton){
			texto = new LabelField(text,USE_ALL_WIDTH | FIELD_HCENTER | DrawStyle.HCENTER);
			texto.setFont(ScreenProperties.normalFieldFontBold);
			add(texto);	
			botonOk = new ButtonField("Ok");
			botonOk.setChangeListener(this);
			add(botonOk);
		}else if(tipo == Previa){
			_animatedBitmapField = new ProgressAnimationField(BitmapManager.getInstance().getSpinner(),6,FIELD_HCENTER | DrawStyle.HCENTER);
			_animatedBitmapField.setMargin(10, 0, 10, 0);
			texto = new LabelField("",USE_ALL_WIDTH | FIELD_HCENTER | DrawStyle.HCENTER);
			texto.setFont(ScreenProperties.normalFieldFontBold);
			add(_animatedBitmapField);
			add(texto);	
			botonVer = new ButtonField(text);
			botonVer.setFont(ScreenProperties.normalFieldFontBold);
			botonVer.setChangeListener(this);
//			agregarNulo();
		}
	}
	
	/*private void agregarNulo() {
		nulo = new NullField(NON_FOCUSABLE);
		add(nulo);
//		texto = new LabelField(text,USE_ALL_WIDTH | FIELD_HCENTER | DrawStyle.HCENTER);
//		texto.setFont(ScreenProperties.normalFieldFontBold);
//		add(texto);	
//		botonOk = new ButtonField("Ok");
//		botonOk.setChangeListener(this);
//		add(botonOk);		
	}*/

	public void colocarBoton(String text){
		if(botonVer.getManager()==null){
			botonVer.setLabel(text);
			add(botonVer);
		}
	}
	
	public void setMessage(String message)
	{
		texto.setText(message);
	}
	
	public boolean onClose() 
	{
		close();
		return true;
	}

	public void fieldChanged(Field field, int context) {
		if(field == botonOk){
			UiApplication.getUiApplication().popScreen(this);
		}else if(field == botonVer){
			quitarBoton();
			UiApplication.getUiApplication().popScreen(this);
		}
	}

	private void quitarBoton() {
		if(botonVer.getManager()!=null){
			delete(botonVer);
		}
	}
	
}
